<template>
  <div>
    <el-row type="flex" justify="space-between">
      <el-col>
        <el-button type="primary" @click="clickCreat"
          >添加优秀团队及领头人</el-button
        >
      </el-col>
      <el-col>
        <div style="display: flex; justify-content: flex-end">
          <el-input
            @clear="clear"
            clearable
            v-model="form.search"
            style="width: 300px; margin-left: 15px"
            placeholder="搜索团队名称、领域、院校"
            class="input-with-select"
          >
            <el-button
              @click="searchVal"
              slot="append"
              style="
                background-color: #45a321;
                color: #fff;
                border-radius: 0;
                border: 1px solid #45a321;
              "
              >搜索</el-button
            >
            <i slot="prefix-icon" class="el-icon-search"></i>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <el-table
      align="center"
      :data="list.list"
      border
      style="width: 100%; margin: 20px 0"
    >
      <el-table-column align="center" label="封面图">
        <template slot-scope="scope">
          <div style="position: relative; width: 100%; padding-top: 56%">
            <img
              :src="scope.row.tpic"
              alt=""
              style="
                width: 100%;
                height: 100%;
                position: absolute;
                left: 0px;
                top: 0px;
              "
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="团队名称">
      </el-table-column>
      <el-table-column align="center" prop="realm" label="领域">
      </el-table-column>
      <el-table-column align="center" prop="school" label="院校">
      </el-table-column>
      <el-table-column align="center" prop="myorder" label="排序">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            class="editText"
            @click="clickEdit(scope.row.id)"
            type="text"
            >修改</el-button
          >
          <el-button
            class="deletText"
            @click="deletUser(scope.row.id)"
            type="text"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="list.page"
        :page-size="list.size"
        layout="total, prev, pager, next, jumper"
        :total="parseInt(list.count)"
        align="left"
      ></el-pagination>
    </div>
    <el-dialog
      title="优秀团队及领头人"
      :visible.sync="dialogVisible"
      width="50%"
      top="10.8vh" 
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <div>
        <div class="s_flex s_align_items news_mrt">
          <p style="width: 15%">封面图</p>
          <div class="left_w s_flex s_align_items">
            <div style="margin-right: 20px">
              <el-upload
                class="upload-demo"
                :on-success="onSuccess"
                :show-file-list="false"
                :action="`${VUE_APP_BASE_API}/v1/index/upload `"
              >
                <!-- -->
                <el-button type="primary">上传封面图</el-button>
              </el-upload>
            </div>
            <el-button v-if="form.tpic" @click="form.tpic = ''" type="danger"
              >删除</el-button
            >
          </div>
        </div>
        <div class="news_mrt" v-if="form.tpic">
          <img style="width: 15%" :src="form.tpic" />
        </div>
        <div class="s_flex s_align_items news_mrt">
          <p style="width: 15%">团队名称</p>
          <div class="left_w">
            <el-input
              v-model="form.name"
              placeholder="请输入团队名称"
            ></el-input>
          </div>
        </div>
        <div class="s_flex s_align_items news_mrt">
          <div class="s_flex s_align_items" style="width: 50%">
            <p style="width: 30%">领域</p>
            <div class="">
              <el-select v-model="form.realm" placeholder="请选择">
                <el-option
                  v-for="item in fieldList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
          </div>
          <div class="s_flex s_align_items" style="width: 50%">
            <p style="width: 30%">院校</p>
            <div class="">
              <el-select v-model="form.school" placeholder="请选择">
                <el-option
                  v-for="item in collegeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
        <div class="s_flex s_align_items news_mrt">
          <p style="width: 15%">排序</p>
          <div class="left_w">
            <el-input
              v-model="form.myorder"
              placeholder="请输入内容"
            ></el-input>
          </div>
        </div>
        <div class="news_mrt">
          <p>详细介绍</p>
          <Editor style="margin-top: 20px" :id="`test19`" v-model="form.desc" />
        </div>
        <div class="news_mrt" style="margin-top: 100px">
          <p>科研项目</p>
          <Editor
            style="margin-top: 20px"
            :id="`test20`"
            v-model="form.project"
          />
        </div>
        <div class="news_mrt" style="margin-top: 100px">
          <el-form
            ref="form"
            :model="form"
            label-width="100px"
            label-position="left"
          >
            <!-- <el-form-item
              prop="name"
              :label="'专利申请号' + (index + 1)"
              v-for="(item, index) in FormArr"
              :key="index"
            >
              <el-input
                type="name"
                @input="changeInput(index)"
                style="width: 80%"
                placeholder="请输入专利申请号"
                v-model="form.patent_list[index].name"
              ></el-input>
              <el-button
                type="primary"
                v-if="FormArr - 1 == index"
                @click="addItem(index)"
                >继续添加</el-button
              >
              <el-button type="danger" v-else @click="deleteItem(index)"
                >删除</el-button
              > -->
              <!-- primary danger-->
            <!-- </el-form-item> -->
          </el-form>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { VUE_APP_BASE_API } from "@/config.js";
import {
  teamList,
  teamAdd,
  teamEdit,
  teamDel,
  teamInfo,
  getLabel,
} from "@/api/adminWeb";
import Editor from "@/components/Editor";
export default {
  data() {
    return {
      VUE_APP_BASE_API,
      list: [],
      dialogVisible: false,
      form: {
        excellent_team_id: "",
        search: "",
        team: "",
        myorder: "",
        name: "",
        tpic: "",
        desc: "",
        project: "",
        realm: "",
        school: "",
        patent_list: [
          {
            name: "",
          },
        ],
      },
      FormArr: 1,
      contentDesc: "",
      clearDesc: 0,
      fileList: [],
      fieldList: [],
      collegeList: [],
    };
  },
  components: {
    Editor,
  },
  mounted() {
    this.getLIst();
    this.labelList();
  },
  methods: {
    async labelList() {
      this.fieldList = await getLabel({ type: 1 });
      this.collegeList = await getLabel({ type: 2 });
    },
    deleteItem(index) {
      this.FormArr--;
      this.form.patent_list.splice(index, 1);
    },
    searchVal() {
      this.getLIst();
    },
    clickCreat() {
      this.dialogVisible = true;
    },
    async clickEdit(id) {
      try {
        let i = [];
        this.form = await teamInfo({
          excellent_team_id: id,
        });
        this.form.patent_list.map((item, b) => {
          this.form.patent_list.map((a, c) => {
            if (b == c && item.name) {
              this.form.patent_list[c].name = item.name;
            }
          });
          if (item) {
            i.push(b);
          }
        });
        this.FormArr = i.length;
        // console.log(this.FormArr);
        this.contentDesc = this.form.content;
        this.dialogVisible = true;
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    deletUser(id) {
      this.$confirm("此操作将删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await teamDel({
              excellent_team_id: id,
            });
            this.getLIst();
            this.$message({
              type: "success",
              message: "删除成功",
            });
          } catch (error) {
            this.$message.error(error.message);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    clear() {
      this.getLIst();
    },
    handleClose() {
      this.clearDesc = 1;
      this.dialogVisible = false;
    },
    async onSuccess(response, file, fileList) {
      if (response) {
        // console.log(response.data.url);
        this.form.tpic = response.data.url;
        this.$nextTick(() => {
          this.form.tpic = response.data.url;
        });
        this.$message.success("上传成功");
      }
    },
    async getLIst(page=1) {
      try {
        this.list = await teamList({
          keyword: this.form.search,
          realm:this.realm,
          page,
        });
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    descVal(val) {
      this.form.desc = val;
    },
    project(val) {
      this.form.project = val;
    },
    async save() {
      try {
        const { myorder, name, tpic, desc, project, realm, school } = this.form;
        if (this.form.id) {
          await teamEdit({
            excellent_team_id: this.form.id,
            myorder,
            name,
            tpic,
            desc,
            project,
            realm,
            school,
            patent_list: this.form.patent_list.map((item) => item.name),
          });
          this.getLIst();
          this.$message.success("添加成功");
          this.dialogVisible = false;
          this.clearDesc = 1;
        } else {
          await teamAdd({
            myorder,
            name,
            tpic,
            desc,
            project,
            realm,
            school,
            patent_list: this.form.patent_list.map((item) => item.name),
          });
          this.$message.success("添加成功");
          this.dialogVisible = false;
          this.clearDesc = 1;
          this.getLIst();
          // this.form = {
          //   team: "",
          //   myorder: "",
          //   name: "",
          //   tpic: "",
          //   desc: "",
          //   project: "",
          //   realm: "",
          //   school: "",
          //   patent_list: [
          //     {
          //       name: "",
          //     },
          //   ],
          // };
        }
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    cancle() {
      this.dialogVisible = false;
      this.clearDesc = 1;
    },
    //每页下拉显示数据
    async handleSizeChange(size) {
      try {
        const resUser = await teamList({
          keywords: this.form.search,
          page: size,
        });
      } catch (e) {
        this.$message.error(e.message);
      }
    },
    //点击第几页
    async handleCurrentChange(currentPage) {
      try {
        this.getLIst(currentPage)
      } catch (e) {
        this.$message.error(e.message);
      }
    },
    changeInput(index) {
      this.form.patent_list[index].message = "";
    },
    addItem(index) {
      if (this.form.patent_list[index].name == "") {
        this.$message.error("请填写专利申请号");
      } else {
        this.FormArr++;
        this.form.patent_list.push({
          name: "",
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.news_mrt {
  margin-bottom: 30px;
}
.left_w {
  width: 80%;
}
</style>